<?php
namespace App\Controllers;

class Travel extends BaseController
{
    public function index()
    {
        // connect to the model
        $places = new \App\Models\Places();
        // retrieve all the records
        $records = $places->findAll();

        $table = new \CodeIgniter\View\Table();

        //Setting table headings
        $headings = $places->fields;
        $displayHeadings = array_slice($headings, 1, 2);
        $table->setHeading(array_map('ucfirst', $displayHeadings));

        //Adding data to our table
        foreach ($records as $record) {
            $nameLink = anchor("travel/showme/$record->id", $record->name);
            $table->addRow($nameLink, $record->description);
        }

        $template = [
            'table_open' => '<table cellpadding="5px" width="50%">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);

        $fields = [
            'title' => 'Travel Destinations',
            'heading' => 'A List of Travel Destinations',
            'footer' => 'Copyright Xavier'
        ];

        // get a template parser
        $parser = \Config\Services::parser();
        // tell it about the substitions
        return $parser->setData($fields)
            ->render('templates\top') .
            $table->generate() .
            $parser->setData($fields)
                ->render('templates\bottom');


    }

    public function showme($id)
    {
        // connect to the model
        $places = new \App\Models\Places();
        // retrieve all the records
        $record = $places->find($id);

        $table = new \CodeIgniter\View\Table();

        $table->addRow(anchor("travel/edit/$id", "Edit"));

        $numHeadings = count($record);
        $headings = $places->fields;
        for ($i = 0; $i < $numHeadings; $i++) {

            if ($headings[$i] == 'image') {
                $table->addRow(ucfirst($headings[$i]), "<img width=300 src=\"/image/" . $record[$headings[$i]] . "\"/>");
            } else
                $table->addRow(ucfirst($headings[$i]), $record[$headings[$i]]);
        }

        // get a template parser
        $parser = \Config\Services::parser();

        $fields = [
            'title' => 'Travel Destination',
            'heading' => $record['name'],
            'footer' => 'Copyright Xavier'
        ];

        $template = [
            'table_open' => '<table cellpadding="5px" width="50%">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
        ];
        $table->setTemplate($template);

        // tell it about the substitions
        return $parser->setData($fields)
            ->render('templates\top') .
            $table->generate() .
            $parser->setData($fields)
                ->render('templates\bottom');
    }

    public function edit($id = NULL)
    {
        $places = new \App\Models\Places();
        $record = $places->find($id);

        $fields = [
            'title' => 'Edit a Destination',
            'heading' => 'Edit a Destination',
            'footer' => 'Copyright Xavier'
        ];

        $parser = \Config\Services::parser();
        return $parser->setData($fields)
            ->render('templates\top') .
            $parser->setData($record)
                ->render('editplace') .
            $parser->setData($fields)
                ->render('templates\bottom');



    }

    public function handle()
    {

        //get from POST
        $places = new \App\Models\Places();
        helper(['form', 'url']);
        $record = $places->find($this->request->getPost(index: 'id'));

        //check if the data is valid
        //if its valid then we save it
        if ($this->validate($places->validationRules)) {
            $places->save([
                'id' => $this->request->getPost('id'),
                'name' => $this->request->getPost('name'),
                'description' => $this->request->getPost('description'),
                'link' => $this->request->getPost('link'),
                'image' => $this->request->getPost('image'),
            ]);
        }

        $fields = [
            'title' => 'Edit a Destination',
            'heading' => 'Edit a Destination',
            'footer' => 'Copyright Xavier'
        ];
        $parser = \Config\Services::parser();

        //Top of page
        $returnView = $parser->setData($fields)->render('templates\top');

        //Body of page
        if (!$this->validate($places->validationRules)) {
            $returnView .= $parser->renderString($this->validator->listErrors());
            $returnView .= $parser->setData($this->request->getPost())
                ->render('editplace');
        } else {
            $returnView .= $parser->renderString("<p>Success!</p>");
            $returnView .= $parser->setData($this->request->getPost())
                ->render('editplace');
        }

        //Bottom of page
        $returnView .= $parser->setData($fields)
            ->render('templates\bottom');

        return $returnView;



        //if its not valid we list the errors
    }
}


